﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogic
{
    public static class CheckRefer
    {
        #region hàng hóa

        #region Chi tiết kho hàng
        static Entities.ChiTietKhoHang[] chitietkhohang;
        public static void ChiTietKhoHang()
        {
            Entities.ChiTietKhoHang temp = new Entities.ChiTietKhoHang();
            temp.HanhDong = "ChiTietHang";
            temp.MaKho = "";
            chitietkhohang = BusinessLogic.SelectAll.SelectChiTietHangHoaTheoKho(temp);
            if (chitietkhohang == null)
                chitietkhohang = new Entities.ChiTietKhoHang[0];
        }
        #endregion

        #region Chi tiết đơn đặt hàng khách hàng
        static Entities.ChiTietDonHangKH[] chitietdondathangkh;
        public static void ChiTietDonHangKH()
        {
            //chitietdondathangkh = new BusinessLogic.ChiTietDonHangKH().Select();
            if (chitietdondathangkh == null)
                chitietdondathangkh = new Entities.ChiTietDonHangKH[0];
        }
        #endregion

        #region Chi tiết đơn đặt hàng nhà cung cấp
        static Entities.ChiTietDonHangNCC[] chitietdondathangncc;
        public static void ChiTietDonHangNCC()
        {
            //chitietdondathangncc = new BusinessLogic.ChiTietDonHangNCC().Select();
            if (chitietdondathangncc == null)
                chitietdondathangncc = new Entities.ChiTietDonHangNCC[0];
        }
        #endregion

        #region Cập nhập giá hàng hóa
        static Entities.ChiTietCapNhatGiaHangHoa[] chitietcapnhatgiahanghoa;
        public static void ChiTietCapNhatGiaHangHoa()
        {
            //chitietcapnhatgiahanghoa = new BusinessLogic.ChiTietCapNhatGiaHangHoa().Select();
            if (chitietcapnhatgiahanghoa == null)
                chitietcapnhatgiahanghoa = new Entities.ChiTietCapNhatGiaHangHoa[0];
        }
        #endregion

        #region Chi tiết bán buôn
        static Entities.ChiTietHDBanBuon[] chitiethdbanbuon;
        public static void ChiTietHDBanBuon()
        {
            chitiethdbanbuon = BusinessLogic.ChiTietHDBanBuon.Select();
            if (chitiethdbanbuon == null)
                chitiethdbanbuon = new Entities.ChiTietHDBanBuon[0];
        }
        #endregion

        #region chi tiết bán lẻ
        static Entities.ChiTietHDBanle[] chitiethdbanle;
        public static void ChiTietHDBanle()
        {
            chitiethdbanle = BusinessLogic.ChiTietHDBanLe.Select();
            if (chitiethdbanle == null)
                chitiethdbanle = new Entities.ChiTietHDBanle[0];
        }
        #endregion

        #region Chi tiết nhà cung cấp
        static Entities.ChiTietHangHoaNhaCungCap[] chitietnhacungcap;
        public static void ChiTietNhaCungCap()
        {
            Entities.ChiTietHangHoaNhaCungCap temp = new Entities.ChiTietHangHoaNhaCungCap();
            temp.HanhDong = "2";
            temp.MaNhaCungCap = "";
            //chitietnhacungcap = new BusinessLogic.ChiTietNhaCungCap().SelectChiTietNhaCungCap(temp);
            if (chitietnhacungcap == null)
                chitietnhacungcap = new Entities.ChiTietHangHoaNhaCungCap[0];

        }
        #endregion

        #endregion

        #region Khách hàng

        #region Đơn đặt hàng khách hàng
        static Entities.DonDatHangKH[] dondathangkh;
        public static void DonDatHangKH()
        {
            //dondathangkh = new BusinessLogic.DonDatHangKH().Select();
            if (dondathangkh == null)
                dondathangkh = new Entities.DonDatHangKH[0];
        }
        #endregion

        #region Phiếu Thu
        static Entities.PhieuThu[] phieuthu;
        public static void PhieuThu()
        {
            phieuthu = BusinessLogic.PhieuThu.Select();
            if (phieuthu == null)
                phieuthu = new Entities.PhieuThu[0];
        }
        #endregion

        #region Phiếu Chi
        static Entities.PhieuChi[] phieuchi;
        public static void PhieuChi()
        {
            phieuchi =  BusinessLogic.PhieuChi.Select();
            if (phieuchi == null)
                phieuchi = new Entities.PhieuChi[0];
        }
        #endregion

        #region Hóa đơn bán lẻ
        static Entities.HDBanLe[] hdbanle;
        public static void HDBanLe()
        {
            hdbanle = BusinessLogic.HDBanLe.Select();
            if (hdbanle == null)
                hdbanle = new Entities.HDBanLe[0];
        }
        #endregion

        #region hóa đơn bán buôn
        static Entities.HDBanBuon[] hdbanbuon;
        public static void HDBanBuon()
        {
            hdbanbuon = BusinessLogic.HDBanBuon.Select();
            if (hdbanbuon == null)
                hdbanbuon = new Entities.HDBanBuon[0];
        }
        #endregion

        #endregion

        #region hương
        #region Nhân Viên
        static Entities.NhanVien[] nhanvien;
        public static void NhanVien()
        {
            Entities.NhanVien temp = new Entities.NhanVien();
            temp.HanhDong = "1";
            nhanvien = BusinessLogic.NhanVien.SelectNhanVien(temp);
            if (nhanvien == null)
                nhanvien = new Entities.NhanVien[0];
        }
        #endregion

        #region chi tiết công nợ ncc
        static Entities.ChiTietCongNoNCC[] chitietcongnoncc;
        public static void ChiTietCongNoNCC()
        {
            Entities.ChiTietCongNoNCC temp = new Entities.ChiTietCongNoNCC();
            temp.HanhDong = "0";
            temp.MaNhaCungCap = "";
            temp.MaKho = "";
            chitietcongnoncc = BusinessLogic.ChiTietCongNoNCC.Select(temp);
            if (chitietcongnoncc == null)
                chitietcongnoncc = new Entities.ChiTietCongNoNCC[0];
        }
        #endregion

        #region Đơn đặt hàng ncc
        static Entities.DonDatHangNCC[] dondathangncc;
        public static void DonDatHangNCC()
        {
           // dondathangncc = new BusinessLogic.DonDatHangNCC().Select();
            if (dondathangncc == null)
                dondathangncc = new Entities.DonDatHangNCC[0];
        }
        #endregion

        #region khách hàng trả lại bán buôn
        static Entities.KhachHangTraLaiBanBuon[] khtlbb;
        public static void KhachHangTraLaiBanBuon()
        {
            Entities.KhachHangTraLaiBanBuon temp = new Entities.KhachHangTraLaiBanBuon();
            temp.HanhDong = "2";
            temp.MaKho = "";
           // khtlbb = new BusinessLogic.KhachHangTraLaiBanBuon().Select(temp);
            if (khtlbb == null)
                khtlbb = new Entities.KhachHangTraLaiBanBuon[0];
        }
        #endregion

        #region khách hàng trả lại bán buôn
        static Entities.KhachHangTraLaiBanLe[] khtlbl;
        public static void KhachHangTraLaiBanLe()
        {
            Entities.KhachHangTraLaiBanLe temp = new Entities.KhachHangTraLaiBanLe();
            temp.HanhDong = "2";
            temp.MaKho = "";
           // khtlbl = new BusinessLogic.KhachHangTraLaiBanLe().Select(temp);
            if (khtlbl == null)
                khtlbl = new Entities.KhachHangTraLaiBanLe[0];
        }
        #endregion

        #region hóa đơn nhập
        static Entities.HoaDonNhap[] hdn;
        public static void HoaDonNhap()
        {
            Entities.HoaDonNhap temp = new Entities.HoaDonNhap();
            temp.HanhDong = "3";
            temp.MaKho = "";
            //hdn = new BusinessLogic.HoaDonNhap().Select(temp);
            if (hdn == null)
                hdn = new Entities.HoaDonNhap[0];

        }
        #endregion

        #region trả lại ncc
        static Entities.TraLaiNCC[] tralaincc;
        public static void TraLaiNCC()
        {
            tralaincc = BusinessLogic.TraLaiNCC.Select();
            if (tralaincc == null)
                tralaincc = new Entities.TraLaiNCC[0];

        }
        #endregion

        #region phiếu thanh toán kh
        static Entities.PhieuTTCuaKH[] phieuttkh;
        public static void PhieuTTCuaKH()
        {
            phieuttkh = BusinessLogic.PhieuTTKH.Select();
            if (phieuttkh == null)
                phieuttkh = new Entities.PhieuTTCuaKH[0];

        }
        #endregion

        #region phiếu thanh toán ncc
        static Entities.PhieuTTNCC[] phieuttncc;
        public static void PhieuTTNCC()
        {
            phieuttncc = BusinessLogic.PhieuTTNCC.Select();
            if (phieuttncc == null)
                phieuttncc = new Entities.PhieuTTNCC[0];

        }
        #endregion

        #region Nhóm hàng
        //Entities.NhomHang[] nhomhang;
        //public static void NhomHang()
        //{
        //    Entities.NhomHang temp = new Entities.NhomHang();
        //    temp.HanhDong = "2";
        //    nhomhang = new BusinessLogic.NhomHang().Select(temp);
        //    if (nhomhang == null)
        //        nhomhang = new Entities.NhomHang[0];
        //}
        #endregion

        #region hàng hóa
        static Entities.HangHoa[] hanghoa;
       
        public static void HangHoa()
        {
            Entities.HangHoa temp = new Entities.HangHoa();
            temp.HanhDong = "1";
            hanghoa = BusinessLogic.HangHoa.SelectHangHoa(temp);
            if (hanghoa == null)
                hanghoa = new Entities.HangHoa[0];
        }
        #endregion

        #region Số Dư Công Nợ
        static Entities.SoDuCongNo[] soducongno;
        public static Entities.SoDuCongNo[] SoDuCongNo()
        {
            try
            {
                soducongno = new Entities.SoDuCongNo[1];
                soducongno = BusinessLogic.CongNo.Select();
                if (soducongno == null)
                {
                    soducongno = new Entities.SoDuCongNo[0];
                    return soducongno;
                }
                return soducongno;
            }
            catch
            {
                return soducongno = new Entities.SoDuCongNo[0];
            }

        }
        #endregion

        #region kho hàng
        static Entities.KhoHang[] khohang;
        public static Entities.KhoHang[] KhoHang()
        {
          //  khohang = new BusinessLogic.KhoHang().Select();
            if (khohang == null)
                khohang = new Entities.KhoHang[0];
            return khohang;
        }
        #endregion

        #endregion

        public static bool CheckReferen(Entities.CheckRefer cr)
        {
            bool kt = true;
            try
            {
                switch (cr.TenTruong)
                {
                    case "HangHoa":
                        {
                            // chi tiết nhà cung cấp
                            ChiTietNhaCungCap();
                            for (int i = 0; i < chitietnhacungcap.Length; i++)
                            {
                                if (chitietnhacungcap[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết kho hàng
                            ChiTietKhoHang();
                            for (int i = 0; i < chitietkhohang.Length; i++)
                            {
                                if (chitietkhohang[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // cập nhật giá hàng hóa
                            ChiTietCapNhatGiaHangHoa();
                            for (int i = 0; i < chitietcapnhatgiahanghoa.Length; i++)
                            {
                                if (chitietcapnhatgiahanghoa[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết đơn đặt hàng
                            ChiTietDonHangKH();
                            for (int i = 0; i < chitietdondathangkh.Length; i++)
                            {
                                if (chitietdondathangkh[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            ChiTietDonHangNCC();
                            for (int i = 0; i < chitietdondathangncc.Length; i++)
                            {
                                if (chitietdondathangncc[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết hóa đơn bán hàng
                            ChiTietHDBanBuon();
                            for (int i = 0; i < chitiethdbanbuon.Length; i++)
                            {
                                if (chitiethdbanbuon[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            ChiTietHDBanle();
                            for (int i = 0; i < chitiethdbanle.Length; i++)
                            {
                                if (chitiethdbanle[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    case "KhachHang":
                        {
                            // đơn đặt hàng
                            DonDatHangKH();
                            for (int i = 0; i < dondathangkh.Length; i++)
                            {
                                if (dondathangkh[i].MaKhachHang == cr.MaTruong)
                                    return false;
                            }
                            // hóa đơn bán hàng
                            HDBanBuon();
                            for (int i = 0; i < hdbanbuon.Length; i++)
                            {
                                if (hdbanbuon[i].MaKhachHang == cr.MaTruong)
                                    return false;
                            }
                            HDBanLe();
                            for (int i = 0; i < hdbanle.Length; i++)
                            {
                                if (hdbanle[i].MaKhachHang == cr.MaTruong)
                                    return false;
                            }
                            // phiếu thu
                            PhieuThu();
                            for (int i = 0; i < phieuthu.Length; i++)
                            {
                                if (phieuthu[i].MaDoiTuong == cr.MaTruong)
                                    return false;
                            }

                            PhieuChi();
                            for (int i = 0; i < phieuchi.Length; i++)
                            {
                                if (phieuchi[i].MaDoiTuong == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #region Hungvv
                    case "NhanVien":
                        {
                            try
                            {
                                Entities.HDBanBuon[] banbuon = null;
                                Entities.HDBanLe[] banle = null;
                                //Entities.KhoHang[] khohang = null;
                                //Entities.HoaDonNhap[] nhapkho = null;
                                //Entities.DonDatHangKH[] dathangkhachhang = null;
                                //Entities.DonDatHangNCC[] dathangnhacungcap = null;
                                //khohang = new BusinessLogic.KhoHang().Select();
                                //if (khohang != null)
                                //{
                                //    for (int i = 0; i < khohang.Length; i++)
                                //    {
                                //        if (khohang[i].MaNhanVien == cr.MaTruong)
                                //            return false;
                                //    }
                                //}
                                //nhapkho = BusinessLogic.HoaDonNhap.SelectHDNhap();
                                //if (nhapkho != null)
                                //{
                                //    for (int i = 0; i < nhapkho.Length; i++)
                                //    {
                                //        if (nhapkho[i].MaNhanVien == cr.MaTruong)
                                //            return false;
                                //    }
                                //}
                                banbuon = BusinessLogic.HDBanBuon.Select();
                                if (banbuon != null)
                                {
                                    for (int i = 0; i < banbuon.Length; i++)
                                    {
                                        if (banbuon[i].MaNhanVien == cr.MaTruong)
                                            return false;
                                    }
                                }
                                banle = BusinessLogic.HDBanLe.Select();
                                if (banle != null)
                                {
                                    for (int i = 0; i < banle.Length; i++)
                                    {
                                        if (banle[i].MaNhanVien == cr.MaTruong)
                                            return false;
                                    }
                                }
                                //dathangkhachhang = BusinessLogic.DonDatHangKH.Select();
                                //if (dathangkhachhang != null)
                                //{
                                //    for (int i = 0; i < dathangkhachhang.Length; i++)
                                //    {
                                //        if (dathangkhachhang[i].MaNhanVien == cr.MaTruong)
                                //            return false;
                                //    }
                                //}
                                //dathangnhacungcap = BusinessLogic.DonDatHangNCC.Select();
                                //if (dathangnhacungcap != null)
                                //{
                                //    for (int i = 0; i < dathangnhacungcap.Length; i++)
                                //    {
                                //        if (dathangnhacungcap[i].MaNhanVien == cr.MaTruong)
                                //            return false;
                                //    }
                                //}
                            }
                            catch { return true; }
                        }
                        break;
                    case "KhoanMucThuChi":
                        {
                            try
                            {
                                Entities.PhieuThu[] phieuthu = BusinessLogic.PhieuThu.Select();
                                Entities.PhieuChi[] phieuchi = BusinessLogic.PhieuChi.Select();

                                if (phieuthu != null)
                                {
                                    for (int i = 0; i < phieuthu.Length; i++)
                                    {
                                        if (phieuthu[i].KhoanMuc == cr.MaTruong)
                                            return false;
                                    }
                                }

                                if (phieuchi != null)
                                {
                                    for (int i = 0; i < phieuchi.Length; i++)
                                    {
                                        if (phieuchi[i].KhoanMuc == cr.MaTruong)
                                            return false;
                                    }
                                }

                            }
                            catch { return true; }
                        }
                        break;
                    case "NhomTaiKhoanKeToan":
                        {
                            try
                            {
                                Entities.TKKeToan row = new Entities.TKKeToan("0");
                                Entities.TKKeToan[] taikhoanketoan = BusinessLogic.TKKeToan.Select(row);
                                if (taikhoanketoan != null)
                                {
                                    for (int i = 0; i < taikhoanketoan.Length; i++)
                                    {
                                        if (taikhoanketoan[i].MaNhomTKKeToan == cr.MaTruong)
                                            return false;
                                    }
                                }
                            }
                            catch { return true; }
                        }
                        break;
                    case "TaiKhoanKeToan":
                        {
                            try
                            {
                                Entities.KMThuChi row = new Entities.KMThuChi("0");
                                Entities.KMThuChi[] khoanmucthuchi =  BusinessLogic.KMThuChi.Select(row);
                                if (khoanmucthuchi != null)
                                {
                                    for (int i = 0; i < khoanmucthuchi.Length; i++)
                                    {
                                        if (khoanmucthuchi[i].CoTK == cr.MaTruong || khoanmucthuchi[i].NoTK == cr.MaTruong)
                                            return false;
                                    }
                                }
                            }
                            catch { return true; }
                        }
                        break;
                    case "NhomHang":
                        {
                            try
                            {
                                Entities.HangHoa[] hanghoa = BusinessLogic.HangHoa.SelectHangHoa();
                                if (hanghoa != null)
                                {
                                    for (int i = 0; i < hanghoa.Length; i++)
                                    {
                                        if (hanghoa[i].MaNhomHang == cr.MaTruong)
                                            return false;
                                    }
                                }
                            }
                            catch { return true; }
                        }
                        break;
                    #endregion

                    #region hương
                    #region phòng ban
                    case "PhongBan":
                        {
                            // nhân viên
                            NhanVien();
                            for (int i = 0; i < nhanvien.Length; i++)
                            {
                                if (nhanvien[i].MaPhongBan == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #endregion

                    #region loại hàng hóa
                    case "LoaiHangHoa":
                        {
                            // nhóm hàng hóa
                            //NhomHang();
                            //for (int i = 0; i < nhomhang.Length; i++)
                            //{
                            //    if (nhomhang[i].MaLoaiHang == cr.MaTruong)
                            //        return false;
                            //}
                            break;
                        }
                    #endregion

                    #region Nhà cung cấp
                    case "NhaCungCap":
                        {
                            //hóa đơn nhập
                            HoaDonNhap();
                            for (int i = 0; i < hdn.Length; i++)
                            {
                                if (hdn[i].MaNhaCungCap == cr.MaTruong)
                                    return false;
                            }
                            // sô dư đầu kỳ công nợ
                            SoDuCongNo();
                            for (int i = 0; i < soducongno.Length; i++)
                            {
                                if (soducongno[i].MaDoiTuong == cr.MaTruong)
                                    return false;
                            }
                            // đơn đặt hàng nhà cung cấp
                            DonDatHangNCC();
                            for (int i = 0; i < dondathangncc.Length; i++)
                            {
                                if (dondathangncc[i].MaNhaCungCap == cr.MaTruong)
                                    return false;
                            }
                            // trả lại nhà cung cấp
                            TraLaiNCC();
                            for (int i = 0; i < tralaincc.Length; i++)
                            {
                                if (tralaincc[i].MaNCC == cr.MaTruong)
                                    return false;
                            }
                            // phiếu thu
                            PhieuThu();
                            for (int i = 0; i < phieuthu.Length; i++)
                            {
                                if (phieuthu[i].DoiTuong == cr.MaTruong)
                                    return false;
                            }
                            // phiếu chi
                            PhieuChi();
                            for (int i = 0; i < phieuchi.Length; i++)
                            {
                                if (phieuchi[i].DoiTuong == cr.MaTruong)
                                    return false;
                            }
                            // phiếu thanh toán của nhà cung cấp
                            PhieuTTNCC();
                            for (int i = 0; i < phieuttncc.Length; i++)
                            {
                                if (phieuttncc[i].MaNhaCungCap == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #endregion

                    #region Thuế
                    case "Thue":
                        {
                            // hàng hóa
                            HangHoa();
                            for (int i = 0; i < hanghoa.Length; i++)
                            {
                                if (hanghoa[i].MaThue == cr.MaTruong)
                                    return false;
                            }
                            // khách hàng trả lại bán buôn
                            KhachHangTraLaiBanBuon();
                            for (int i = 0; i < khtlbb.Length; i++)
                            {
                                if (khtlbb[i].Thue == cr.MaTruong)
                                    return false;
                            }
                            // trả lại nhà cung cấp
                            TraLaiNCC();
                            for (int i = 0; i < tralaincc.Length; i++)
                            {
                                if (tralaincc[i].ThueGTGT == cr.MaTruong)
                                    return false;
                            }
                            // nhập kho
                            HoaDonNhap();
                            for (int i = 0; i < hdn.Length; i++)
                            {
                                if (hdn[i].ThueGTGT == cr.MaTruong)
                                    return false;
                            }
                            // hóa đơn bán buôn
                            HDBanBuon();
                            for (int i = 0; i < hdbanbuon.Length; i++)
                            {
                                if (hdbanbuon[i].ThueGTGT == cr.MaTruong)
                                    return false;
                            }
                            // hóa đơn bán lẻ
                            HDBanLe();
                            for (int i = 0; i < hdbanle.Length; i++)
                            {
                                if (hdbanle[i].ThueGTGT == cr.MaTruong)
                                    return false;
                            }
                            //chi tiết đơn đặt hàng khách hàng
                            ChiTietDonHangKH();
                            for (int i = 0; i < chitietdondathangkh.Length; i++)
                            {
                                if (chitietdondathangkh[i].Thue == cr.MaTruong)
                                    return false;
                            }
                            //chi tiết đơn đặt hàng nhà cung cấp
                            ChiTietDonHangNCC();
                            for (int i = 0; i < chitietdondathangncc.Length; i++)
                            {
                                if (chitietdondathangncc[i].Thue == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #endregion

                    #region Đơn vị tính
                    case "DVT":
                        {
                            // hàng hóa
                            HangHoa();
                            for (int i = 0; i < hanghoa.Length; i++)
                            {
                                if (hanghoa[i].MaDonViTinh == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #endregion

                    #region gói hàng
                    case "GoiHang":
                        {
                            // hàng hóa
                            HangHoa();
                            for (int i = 0; i < hanghoa.Length; i++)
                            {
                                if (hanghoa[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết đơn đặt hàng
                            ChiTietDonHangKH();
                            for (int i = 0; i < chitietdondathangkh.Length; i++)
                            {
                                if (chitietdondathangkh[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            ChiTietDonHangNCC();
                            for (int i = 0; i < chitietdondathangncc.Length; i++)
                            {
                                if (chitietdondathangncc[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết hóa đơn bán hàng
                            ChiTietHDBanBuon();
                            for (int i = 0; i < chitiethdbanbuon.Length; i++)
                            {
                                if (chitiethdbanbuon[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            ChiTietHDBanle();
                            for (int i = 0; i < chitiethdbanle.Length; i++)
                            {
                                if (chitiethdbanle[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #endregion

                    #region quy đổi đơn vị tính
                    case "QuyDoiDonViTinh":
                        {
                            // khách hàng trả lại bán buôn
                            KhachHangTraLaiBanBuon();
                            for (int i = 0; i < khtlbb.Length; i++)
                            {
                                if (khtlbb[i].Thue == cr.MaTruong)
                                    return false;
                            }
                            // trả lại nhà cung cấp
                            TraLaiNCC();
                            for (int i = 0; i < tralaincc.Length; i++)
                            {
                                if (tralaincc[i].ThueGTGT == cr.MaTruong)
                                    return false;
                            }
                            // hàng hóa
                            HangHoa();
                            for (int i = 0; i < hanghoa.Length; i++)
                            {
                                if (hanghoa[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết đơn đặt hàng
                            ChiTietDonHangKH();
                            for (int i = 0; i < chitietdondathangkh.Length; i++)
                            {
                                if (chitietdondathangkh[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            ChiTietDonHangNCC();
                            for (int i = 0; i < chitietdondathangncc.Length; i++)
                            {
                                if (chitietdondathangncc[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            // chi tiết hóa đơn bán hàng
                            ChiTietHDBanBuon();
                            for (int i = 0; i < chitiethdbanbuon.Length; i++)
                            {
                                if (chitiethdbanbuon[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            ChiTietHDBanle();
                            for (int i = 0; i < chitiethdbanle.Length; i++)
                            {
                                if (chitiethdbanle[i].MaHangHoa == cr.MaTruong)
                                    return false;
                            }
                            //hóa đơn nhập
                            HoaDonNhap();
                            for (int i = 0; i < hdn.Length; i++)
                            {
                                if (hdn[i].MaNhaCungCap == cr.MaTruong)
                                    return false;
                            }
                            break;
                        }
                    #endregion
                    #endregion
                }
                return kt;
            }
            catch
            {
                return false;
            }
        }
    }
}
#endregion